﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Superstore.Store.App_Code
{
    public class TimerHelper
    {
        /// <summary>
        /// 将当前C#时间转化为UTC毫秒数
        /// </summary>
        /// <returns></returns>
        public static long CurrentTimeToUTC()
        {
            DateTime dt_1970 = new DateTime(1970, 1, 1);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            DateTime time = DateTime.Now;
            DateTime currentTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0);
            long ticksCurrentTime = currentTime.Ticks;
            long time_UTC = (ticksCurrentTime - tricks_1970) / 10000;
            return time_UTC;
        }
        /// <summary>
        /// 将UTC毫秒数转化为C#时间
        /// </summary>
        /// <returns></returns>
        public static DateTime UTCToTime(long UTC_time)
        {
            //long time_JAVA_Long1 = 1432828800000;//java长整型日期，毫秒为单位
            DateTime dt_1970 = new DateTime(1970, 1, 1);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + UTC_time * 10000;//日志日期刻度
            DateTime time = new DateTime(time_tricks);
            return time;
        }

        /// <summary>
        /// 显示规则：
        /// 1.如果送货时间在当前时间的一个小时之内，显示“一小时内送达”
        /// 2.如果送货时间是当前时间的明天或者后天，显示“明天/后天+具体时间”
        /// 3.如果送货时间不是上面的情况，显示具体的时间“年月日小时”
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static string getDeliveryTime(long start, long end)
        {
            DateTime startTime = UTCToTime(start);
            DateTime endTime = UTCToTime(end);
            if (startTime.Year == DateTime.Now.Year && startTime.Month == DateTime.Now.Month && startTime.Day == DateTime.Now.Day && startTime.Hour == DateTime.Now.Hour)
            {
                return "一个小时内送达";
            }
            else if (startTime.Year == DateTime.Now.Year && startTime.Month == DateTime.Now.Month && (startTime.Day == (DateTime.Now.Day + 1)))
            {
                string start_time = startTime.ToString("HH:mm");
                string end_time = endTime.ToString("-HH:mm");
                string result = "明天 " + (start_time + end_time).ToString();
                return result;
            }
            else if (startTime.Year == DateTime.Now.Year && startTime.Month == DateTime.Now.Month && (startTime.Day == (DateTime.Now.Day + 2)))
            {
                string start_time = startTime.ToString("HH:mm");
                string end_time = endTime.ToString("-HH:mm");
                string result = "后天 " + (start_time + end_time).ToString();
                return result;
            }
            else
            {
                string start_time = startTime.ToString("yyyy-MM-dd HH:mm");
                string end_time = endTime.ToString("-HH:mm");
                string result = (start_time + end_time).ToString();
                return result;
            }
        }
    }
}